# 模型评估与使用
import sys
import torch
import os

module_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(module_dir)



# 示例文本数据
text = "hello world my name is liyishan hello"
# 分词
tokens = text.split()
# 构建词汇表
vocab = sorted(set(tokens))
vocab_size = len(vocab)
# 单词到索引的映射
word_to_idx = {word: idx for idx, word in enumerate(vocab)}
# 索引到单词的映射
idx_to_word = {idx: word for idx, word in enumerate(vocab)}


# 准备测试输入
test_input = torch.tensor([word_to_idx["hello"]], dtype=torch.long)
with torch.no_grad():
    model = torch.load('saved_model.pth',weights_only=False)

    output = model(test_input)

    predicted_idx = torch.argmax(output).item()
    predicted_word = idx_to_word[predicted_idx]
    print(f'Input: "hello", Predicted next word: {predicted_word}')